package com.zzb.projectfactory.framework.role.controller;

import com.zzb.projectfactory.framework.common.BaseController;
import com.zzb.projectfactory.framework.role.entity.SysRole;
import com.zzb.projectfactory.framework.role.entity.User;
import com.zzb.projectfactory.framework.role.form.SysRoleSearchForm;
import com.zzb.projectfactory.framework.role.service.SysRoleService;
import com.zzb.projectfactory.framework.common.BasePage;
import com.zzb.projectfactory.framework.common.BaseResponse;
import com.zzb.projectfactory.framework.common.ServiceException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 角色表controller
 */
@Controller
@RequestMapping("/system/sysRole")
public class SysRoleController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(SysRoleController.class);
    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 角色表管理首页
     */
    @RequestMapping({"", "/", "/index"})
    public String index() {
        return "system/sysRole/index";
    }

    /**
     * 角色表列表json
     */
    @RequestMapping("list")
    @ResponseBody
    public BasePage<SysRole> list(SysRoleSearchForm sysRoleSearchForm) {
        return sysRoleService.queryPage(sysRoleSearchForm, SysRole.class);
    }

    /**
     * 角色表表单页面
     */
    @RequestMapping("form")
    public String form(String id, Model model) {
        SysRole sysRole;
        if (StringUtils.isEmpty(id)) {
            sysRole = new SysRole();
        } else {
            sysRole = sysRoleService.get(id);
            if (sysRole == null) sysRole = new SysRole();
        }
        model.addAttribute("sysRole", sysRole);
        return "system/sysRole/form";
    }

    /**
     * 角色表保存
     */
    @RequestMapping("/save")
    @ResponseBody
    public BaseResponse<SysRole> save(SysRole sysRole, HttpServletRequest request) {
        try {
            User user = getCurrentUser(request);
            return sysRoleService.saveRole(sysRole, user);
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse<>(BaseResponse.FAIL, e.getMessage());
        }
    }

    /**
     * 角色表删除
     */
    @RequestMapping("/delete")
    @ResponseBody
    public BaseResponse delete(String id, HttpServletRequest request) {
        try {
            return sysRoleService.deleteRole(id, getCurrentUser(request));
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL, e.getMessage());
        }
    }

    /**
     * 角色表开启
     */
    @RequestMapping("/open")
    @ResponseBody
    public BaseResponse open(String id, HttpServletRequest request) {
        try {
            return sysRoleService.recovery(id, getCurrentUser(request));
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL, e.getMessage());
        }
    }

    /**
     * 角色表屏蔽
     */
    @RequestMapping("/deny")
    @ResponseBody
    public BaseResponse deny(String id, HttpServletRequest request) {
        try {
            return sysRoleService.trash(id, getCurrentUser(request));
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL, e.getMessage());
        }
    }

}
